<!DOCTYPE html>
<html>
<head>
    <title>Inverting Op-Amp Circuit</title>
</head>
<body>
    <canvas id="circuitCanvas" width="700" height="500"></canvas>
    <script>
        const canvas = document.getElementById('circuitCanvas');
        const ctx = canvas.getContext('2d');

        // --- Style and Configuration ---
        ctx.lineWidth = 2;
        ctx.strokeStyle = 'black';
        ctx.fillStyle = 'black';
        const componentFont = '18px sans-serif';
        const labelFont = 'italic 20px sans-serif';
        const figFont = '16px sans-serif';

        // --- Helper Functions ---
        function drawResistor(x, y, w, h, value) {
            ctx.strokeRect(x, y, w, h);
            ctx.font = componentFont;
            ctx.textAlign = 'center';
            ctx.textBaseline = 'bottom';
            ctx.fillText(value, x + w / 2, y - 5);
        }

        function drawTerminal(x, y) {
            ctx.beginPath();
            ctx.arc(x, y, 6, 0, 2 * Math.PI);
            ctx.fillStyle = 'white';
            ctx.fill();
            ctx.stroke();
            ctx.fillStyle = 'black';
        }

        function drawJunction(x, y) {
            ctx.beginPath();
            ctx.arc(x, y, 4, 0, 2 * Math.PI);
            ctx.fill();
        }

        function drawGround(x, y) {
            drawJunction(x, y);
            const line_v = 5;
            const line1_w = 25;
            const line2_w = 17;
            const line3_w = 9;
            const spacing = 7;
            ctx.beginPath();
            ctx.moveTo(x, y);
            ctx.lineTo(x, y + line_v);
            ctx.moveTo(x - line1_w/2, y + line_v);
            ctx.lineTo(x + line1_w/2, y + line_v);
            ctx.moveTo(x - line2_w/2, y + line_v + spacing);
            ctx.lineTo(x + line2_w/2, y + line_v + spacing);
            ctx.moveTo(x - line3_w/2, y + line_v + 2*spacing);
            ctx.lineTo(x + line3_w/2, y + line_v + 2*spacing);
            ctx.stroke();
        }

        // --- Coordinates ---
        const y_ground = 420;
        const y_inverting = 215;
        const y_opamp_center = 250;
        const y_non_inverting = 285;
        const y_feedback = 100;
        
        const x_in_term = 100;
        const x_r_in_start = 120;
        const x_r_in_end = 220;
        const x_junction_in = 250;
        const x_opamp_in = 300;
        const x_opamp_out = 480;
        const x_junction_out = 510;
        const x_out_term = 600;

        // --- Main Drawing Logic ---

        // Main ground line
        ctx.beginPath();
        ctx.moveTo(50, y_ground);
        ctx.lineTo(650, y_ground);
        ctx.stroke();

        // Op-Amp Body
        ctx.beginPath();
        ctx.moveTo(x_opamp_in, 180);
        ctx.lineTo(x_opamp_out, y_opamp_center);
        ctx.lineTo(x_opamp_in, 320);
        ctx.closePath();
        ctx.stroke();

        // Op-Amp Inputs and Connections
        // Inverting input
        ctx.beginPath();
        ctx.moveTo(x_junction_in, y_inverting);
        ctx.lineTo(x_opamp_in, y_inverting);
        ctx.stroke();
        // Non-inverting input to ground
        ctx.beginPath();
        ctx.moveTo(x_opamp_in, y_non_inverting);
        ctx.lineTo(x_opamp_in, y_ground);
        ctx.stroke();
        drawJunction(x_opamp_in, y_ground);
        
        // Op-Amp Symbols (+, -)
        ctx.font = 'bold 28px sans-serif';
        ctx.textAlign = 'center';
        ctx.textBaseline = 'middle';
        ctx.fillText('-', x_opamp_in + 20, y_inverting);
        ctx.fillText('+', x_opamp_in + 20, y_non_inverting);
        
        // Power Supply Labels
        ctx.font = componentFont;
        const x_power = 390;
        // +6V
        ctx.beginPath();
        ctx.moveTo(x_power, 180);
        ctx.lineTo(x_power, 160);
        ctx.stroke();
        ctx.textBaseline = 'bottom';
        ctx.fillText('+6 V', x_power, 155);
        // -6V
        ctx.beginPath();
        ctx.moveTo(x_power, 320);
        ctx.lineTo(x_power, 340);
        ctx.stroke();
        ctx.textBaseline = 'top';
        ctx.fillText('−6 V', x_power, 345); // Using minus sign '−'

        // Input Path
        drawTerminal(x_in_term, y_inverting);
        ctx.beginPath();
        ctx.moveTo(x_in_term, y_inverting);
        ctx.lineTo(x_r_in_start, y_inverting);
        ctx.stroke();
        drawResistor(x_r_in_start, y_inverting - 10, x_r_in_end - x_r_in_start, 20, '800 Ω');
        ctx.beginPath();
        ctx.moveTo(x_r_in_end, y_inverting);
        ctx.lineTo(x_junction_in, y_inverting);
        ctx.stroke();
        drawJunction(x_junction_in, y_inverting);

        // Feedback Path
        ctx.beginPath();
        ctx.moveTo(x_junction_in, y_inverting);
        ctx.lineTo(x_junction_in, y_feedback);
        ctx.stroke();
        drawResistor(x_junction_in, y_feedback - 10, x_junction_out - x_junction_in, 20, '9600 Ω');
        ctx.beginPath();
        ctx.moveTo(x_junction_out, y_feedback);
        ctx.lineTo(x_junction_out, y_opamp_center);
        ctx.stroke();

        // Output Path
        drawJunction(x_junction_out, y_opamp_center);
        ctx.beginPath();
        ctx.moveTo(x_opamp_out, y_opamp_center);
        ctx.lineTo(x_junction_out, y_opamp_center);
        ctx.moveTo(x_junction_out, y_opamp_center);
        ctx.lineTo(x_out_term, y_opamp_center);
        ctx.stroke();
        drawTerminal(x_out_term, y_opamp_center);
        
        // Ground Symbol
        drawGround(550, y_ground);

        // V_IN and V_OUT Arrows and Labels
        const arrow_head_len = 10;
        // V_IN
        const vin_arrow_x = 70;
        ctx.beginPath();
        ctx.moveTo(vin_arrow_x, y_ground);
        ctx.lineTo(vin_arrow_x, y_inverting);
        ctx.moveTo(vin_arrow_x, y_inverting);
        ctx.lineTo(vin_arrow_x - 5, y_inverting + arrow_head_len);
        ctx.moveTo(vin_arrow_x, y_inverting);
        ctx.lineTo(vin_arrow_x + 5, y_inverting + arrow_head_len);
        ctx.stroke();
        ctx.textAlign = 'right';
        ctx.font = labelFont;
        ctx.fillText('V', vin_arrow_x - 10, (y_ground + y_inverting) / 2);
        ctx.font = 'italic 14px sans-serif';
        ctx.fillText('IN', vin_arrow_x, (y_ground + y_inverting) / 2 + 8);
        
        // V_OUT
        const vout_arrow_x = 630;
        ctx.beginPath();
        ctx.moveTo(vout_arrow_x, y_ground);
        ctx.lineTo(vout_arrow_x, y_opamp_center);
        ctx.moveTo(vout_arrow_x, y_opamp_center);
        ctx.lineTo(vout_arrow_x - 5, y_opamp_center + arrow_head_len);
        ctx.moveTo(vout_arrow_x, y_opamp_center);
        ctx.lineTo(vout_arrow_x + 5, y_opamp_center + arrow_head_len);
        ctx.stroke();
        ctx.textAlign = 'left';
        ctx.font = labelFont;
        ctx.fillText('V', vout_arrow_x + 10, (y_ground + y_opamp_center) / 2);
        ctx.font = 'italic 14px sans-serif';
        ctx.fillText('OUT', vout_arrow_x + 19, (y_ground + y_opamp_center) / 2 + 8);
        
        // Figure Caption
        ctx.font = figFont;
        ctx.textAlign = 'center';
        ctx.textBaseline = 'bottom';
        ctx.fillText('Fig. 8.1', canvas.width / 2, canvas.height - 5);

    </script>
</body>
</html>